package validate

import (
	"fmt"
	"github.com/go-playground/validator/v10"
)

type Address struct {
	Province string `validate:"required"`
	City     string `validate:"required"`
}
type ContactUser struct {
	Name    string   `validate:"required,alphaunicode"`
	Age     uint8    `validate:"gte=20,lte=130"`
	Phone   string   `validate:"required_without_all=Email Address,omitempty,e164"`
	Email   string   `validate:"required_without_all=Phone Address,omitempty,email"`
	Address *Address `validate:"required_without_all=Phone Email"`
}
type User struct {
	Name            string         `validate:"required,alphaunicode"`
	Age             uint8          `validate:"gte=10,lte=130"`
	Phone           string         `validate:"required,e164"`
	Email           string         `validate:"required,email"`
	FavouriteColor1 string         `validate:"iscolor"` // alias for 'hexcolor|rgb|rgba|hsl|hsla'
	FavouriteColor2 string         `validate:"hexcolor|rgb|rgba|hsl|hsla"`
	Address         *Address       `validate:"required"` // a person can have a home and cottage...
	ContactUser     []*ContactUser `validate:"required,gte=1,dive"`
	Hobby           []string       `validate:"required,gte=2,dive,required,gte=2,alphaunicode"`
}

func StructValidate() {
	contactAddress1 := &Address{
		Province: "湖南",
		City:     "长沙",
	}
	contactUser1 := &ContactUser{
		Name: "张三",
		Age:  30,
		//Phone:   "+8613800138000",
		//Email:   "nick@0voice.com",
		Address: contactAddress1,
	}
	contactAddress2 := &Address{
		Province: "湖南",
		City:     "长沙",
	}
	contactUser2 := &ContactUser{
		Name:  "张三",
		Age:   30,
		Phone: "+8613800138000",
		//Email:   "nick@0voice.com",
		Address: contactAddress2,
	}
	address := &Address{
		Province: "湖南",
		City:     "长沙",
	}
	user := &User{
		Name:            "nick",
		Age:             18,
		Phone:           "+8613800138000",
		Email:           "nick@0voice.com",
		FavouriteColor1: "#ffff",
		FavouriteColor2: "rgb(255,255,255)",
		Address:         address,
		ContactUser:     []*ContactUser{contactUser1, contactUser2},
		Hobby:           []string{"乒乓球", "羽毛球"},
	}
	err := validate.Struct(user)
	if err != nil {
		if errors, ok := err.(validator.ValidationErrors); ok {
			for _, err := range errors {
				fmt.Println(err)
			}
		}

	}
}
